package com.srunpass.algorithm;

/**
 * @author srunpass
 * @version Id: 分割汇文串II, v 0.1 2024/6/26 09:56 srunpass Exp $
 */
public class 分割回文串II {

    //回文数据
    Boolean[][] map;
    int len;
    int[] res;


    public int minCut(String s) {
        len = s.length();
        res = new int[len];
        gene(s);
        for (int i = 0; i < len; i++) {
            if (i == 0 || map[0][i]) {
                res[i] = 0;
            } else {
                res[i] = Integer.MAX_VALUE - 1;
                for (int j = i; j >= 1; j--) {
                    if (map[j][i]) {
                        res[i] = Math.min(res[i], res[j - 1] + 1);
                    }
                }
            }
        }
        return res[len - 1];

    }

    private void gene(String s) {

        map = new Boolean[len][len];
        for (int i = len - 1; i >= 0; i--) {
            for (int j = 0; j < len; j++) {
                if (i >= j) {
                    map[i][j] = true;
                } else {
                    if (s.charAt(i) == s.charAt(j)) {
                        map[i][j] = map[i + 1][j - 1];
                    } else {
                        map[i][j] = false;
                    }
                }
            }
        }
    }


}
